﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace studentmanagementdemo
{
    public partial class frmlogin : Form
    {

        private SqlConnection con;
        private DataTable dt = new DataTable("User");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;

        private void connect()
        {
            //String cn = "Data Source=127.0.0.1;Initial Catalog=iPMAC;Persist Security Info=True;User ID=sa;Password=sa";
            String cn = "Data Source=(local);Initial Catalog=iPMAC;Integrated Security=True";
            try
            {
                con = new SqlConnection(cn);
                con.Open(); //Moi ket noi                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối tới cơ sở dữ liệu !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void disconnect()
        {
            con.Close(); //Dong ket noi
            con.Dispose(); //Giai phong tai nguyen
            con = null; //Huy doi tuong
        }
        public frmlogin()
        {
            InitializeComponent();
        }

        private void frmlogin_Load(object sender, EventArgs e)
        {
            connect();
            txtname.Text = "admin";
            this.AcceptButton = btndangnhap;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "THOÁT", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Close();
                System.Windows.Forms.Application.Exit();
            }
        }

        private void btndangnhap_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand(); //Khai bao 1 command
            command.Connection = con; //Ket noi
            command.CommandType = CommandType.Text; //Khai bao kieu command
            command.CommandText = @"Select * From tblUser 
                                             Where (fldName = @fldName)
                                             And (fldPass = @fldPass)"; //Cau truy van SQL
            command.Parameters.Add("@fldName", SqlDbType.NVarChar, 50).Value = txtname.Text;
            command.Parameters.Add("@fldPass", SqlDbType.NVarChar, 50).Value = txtpass.Text;
            da.SelectCommand = command; //Gan command cho da            
            da.Fill(dt); //Nap du lieu vao table

            if (dt.Rows.Count > 0)
            {
                frmtrangchinh _frmtrangchinh = new frmtrangchinh();
                _frmtrangchinh.Show();
                Hide();
            }
            else
            {
                if (MessageBox.Show("Đăng nhập thất bại, bạn có muốn đăng nhập lại không ?", "ĐĂNG NHẬP", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    txtname.Focus();
                }
                else
                {
                    Close();
                    System.Windows.Forms.Application.Exit();
                }
            }
        }
    }
}
